/*site app*/
jQuery(function($){
	/*home setting height*/
	var articleSetHeightApp = {
		setArticle: function(winH){
			var that = this;			
			$('.J-home').find('article').height(winH);
			$('#restaurants').height(winH);
			$('#gallery').height(winH);
			$('#media').height(winH);
			$('#contact').height(winH);
			/*products*/
			$('#products-list-item').height(winH);
			/*gift*/
			var tempH = winH - $('.J-gift-list').find('.gift-list-ad').height();
			$("#gift-list-item").height(tempH);
			
			var aboutH = $('.J-about-banner').find('.about-banner-img').height()-4;
			$('.J-about-wrap').css({'top':aboutH});
		},
		init: function(){
			var that = this;
			var winH = $(window).height()-120;
			that.setArticle(winH);
			$(window).on('resize',function(){
				setTimeout(function (){
					var tempH = $(window).height()-120;					
					that.setArticle(tempH);
				},30);
			});
		}
	};

	/*画布队列*/
	function setQueueStart(element,key){
		var FUNC = [
			function (){
				setTimeout(function(){
					$(element).addClass('act');
					clearQueue();
				},300);	
			},
			function (){
				setTimeout(function(){
					$(element).addClass('show');
					clearQueue();
				},300);	
			},
			function (){
				setTimeout(function(){
					$(element).addClass('anim');
					clearQueue();
				},300);	
			},
			function (){
				setTimeout(function(){
					$(element).addClass('full');
					$('.J-home').addClass('fn-hide');										
					animEnd(key);					
					clearQueue();
				},300);	
			}
		];
		var clearQueue = function() {
			$(document).dequeue("myAnimation");
		};
		$(document).queue("myAnimation",FUNC);
		clearQueue();
	}
	function setQueueEnd(element,key){
		var FUNC = [
			function (){
				setTimeout(function(){
					$(element).removeClass('full');
					$('.J-home').removeClass('fn-hide');
					clearQueue();
				},300);	
			},
			function (){
				setTimeout(function(){
					$(element).removeClass('anim');
					clearQueue();
				},300);	
			},
			function (){
				setTimeout(function(){
					$(element).removeClass('show');
					clearQueue();
				},300);	
			},
			function (){
				setTimeout(function(){
					$(element).removeClass('act');					
					clearQueue();
				},300);	
			}
		];
		var clearQueue = function() {
			$(document).dequeue("myAnimation");
		};
		$(document).queue("myAnimation",FUNC);
		clearQueue();
	}
	/*动画结束调用*/
	function animEnd(key){
		if($('#'+key).is(":hidden")){
			$('#'+key).show();	
		};
		
		if($('#wrapper').hasClass("fn-hide")){
			$('#wrapper').removeClass('fn-hide');	
		};
		
		if(key == 'media'){
			/*gift*/
			var tempAdH = $(window).height() - 120 - $('.J-gift-list').find('.gift-list-ad').height();
			$("#gift-list-item").height(tempAdH);
				
			var tempH = $("#gift-list-item").find('>ul').height();
			$("#gift-list-item").mouseslide({height:tempH});
					
		}
		
		if(key == 'gallery'){
			//最好用ajax去调去数据这里数据太多,ajax回调成功调用
			$("#products-list-item").mouseslide();	
		}
	}
	
	var defaultPayKey = $('body').data('key');
	if(defaultPayKey){
		animEnd(defaultPayKey);
		articleSetHeightApp.init();
	} else {
		articleSetHeightApp.init();
	}
	
	/*home 点击效果*/
	$('.J-home').find('.home-a').on('click',function(){
		var key = $(this).data('key');
		setQueueStart('#'+key,key);	
	});
	
	/*自主产品*/
	/*close 点击效果*/
	$('#gallery').find('.close').on('click',function(){
		var key = $(this).data('key');
		setQueueEnd('#'+key);
	});
	
	/*礼品卡*/
	/*close 点击效果*/
	$('#media').find('.close').on('click',function(){
		var key = $(this).data('key');
		setQueueEnd('#'+key);
	});
	var dialogDetailsBool = true;
	
	$('#products-list-item').find('li').on('click',function(){
		var id = $(this).data("id");
		dialogDetails(id);
	});	
	function dialogDetails(id){
		var $dialogDetails = $('.J-dialog-details').find('.dialog-details');
		var dialogH = $dialogDetails.height();
		var dialogW = $dialogDetails.width();	
		var Touchable = Modernizr.touch || window.navigator.userAgent.indexOf("Touch") != -1;
		if(Touchable){
			$('.J-dialog-details-close').css({"top":0,"right":"25px","background-color":"#d2d2d2"});
			$('.J-dialog-details').find('.details-cnt').css({"padding-right":"10px"});
		}
		var pagei = $.layer({
			type:1,
			border: [0],
			area: [dialogW, dialogH],  
			title: false,
			shade : [0.5 , '#646464' , true],
			/*move: ['.dialog-details', true],*/
			closeBtn: false,
			offset:[
			(function(){
				var winH = $(window).height();
				var temp = (winH - dialogH)/2;
				if(temp < 0) {
					temp = 0;
				}
				return temp;
			})()
			,'50%'],
			page: {dom: '.J-dialog-details'},
			success: function(data){
				/*ajax数据*/
				/*DEMO*/
				$.ajax({
					type: "POST",
					url: "ajax.html",
					async:true,
					data: { id: id },
					success: function(data){
						$('.details-cnt').html(data);
						dialogSlider('.J-main-visual','.J-main-image',true);
					}
				});
			
				var page = $('.J-dialog-details-close');
				page.on('click', function(){
					layer.close(pagei);
				});
				
				
			}
		});	
		
	}
});